#include "java_report.h"
#include "../crypt/crypt.h"
#include "com_dhs_report_FrReport.h"
#include "../util.h"


void throwJavaException(JNIEnv *env, const char *msg)
{
    jclass exClass = env->FindClass("java/lang/Exception");
    if(exClass==NULL){
        return;
    }
    // jmethodID exMethod = env->GetMethodID(exClass, "<init>", "(Ljava/lang/String;)V");
    // if(exMethod==NULL){
    //     return;
    // }
    jstring exString = env->NewStringUTF(msg);

    env->ThrowNew(exClass, msg);

     env->DeleteLocalRef(exString);
}
 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved){
    return JNI_VERSION_1_8;
 }

jbyteArray JNICALL Java_com_dhs_report_FrReport_encReport(JNIEnv * env, jclass, jbyteArray srcData){

    jsize dataLen = env->GetArrayLength(srcData);
    if(dataLen==0){
     throwJavaException(env, "要加密的数据不能为空");
        return NULL;
    }

    jbyteArray destArray = env->NewByteArray(dataLen);
    jbyte *dstbyte = env->GetByteArrayElements(destArray, NULL);

    
    jbyte *srcbyte = env->GetByteArrayElements(srcData, NULL);

    BEGIN_EXCEPT


    encrypt((char *)srcbyte, (char *)dstbyte, dataLen);

    END_EXCEPT
    env->ReleaseByteArrayElements(srcData, srcbyte, 0);
    env->ReleaseByteArrayElements(destArray, dstbyte, 0);

    return destArray;
}